package com.ycuwq.datepicker.time;

import com.ycuwq.datepicker.WheelPicker;
import ohos.agp.components.AttrSet;
import ohos.app.Context;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * MinutePicker
 * Created by ycuwq on 2018/1/22.
 */
public class MinutePicker extends WheelPicker<Integer> {
    /**
     * The constant M on minute selected listener
     */
    private OnMinuteSelectedListener mOnMinuteSelectedListener;

    /**
     * Minute picker
     *
     * @param context context
     */
    public MinutePicker(Context context) {
        this(context, null);
    }

    /**
     * Minute picker
     *
     * @param context context
     * @param attrs   attrs
     */
    public MinutePicker(Context context, AttrSet attrs) {
        this(context, attrs, null);
    }

    /**
     * Minute picker
     *
     * @param context      context
     * @param attrs        attrs
     * @param defStyleAttr def style attr
     */
    public MinutePicker(Context context, AttrSet attrs, String defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setItemMaximumWidthText("00");
        NumberFormat numberFormat = NumberFormat.getNumberInstance();
        numberFormat.setMinimumIntegerDigits(2);
        setDataFormat(numberFormat);
        updateMinute();
        setOnWheelChangeListener(new OnWheelChangeListener<Integer>() {
            @Override
            public void onWheelSelected(Integer item, int position) {
                if (mOnMinuteSelectedListener != null) {
                    mOnMinuteSelectedListener.onMinuteSelected(item);
                }
            }
        });
    }

    /**
     * Update minute
     */
    private void updateMinute() {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 60; i++) {
            list.add(i);
        }
        setDataList(list);
    }

    /**
     * Set selected minute *
     *
     * @param hour hour
     */
    public void setSelectedMinute(int hour) {
        setSelectedMinute(hour, true);
    }

    /**
     * Set selected minute *
     *
     * @param hour        hour
     * @param smootScroll smoot scroll
     */
    public void setSelectedMinute(int hour, boolean smootScroll) {
        setCurrentPosition(hour, smootScroll);
    }

    /**
     * Set on minute selected listener *
     *
     * @param onMinuteSelectedListener on minute selected listener
     */
    public void setOnMinuteSelectedListener(OnMinuteSelectedListener onMinuteSelectedListener) {
        mOnMinuteSelectedListener = onMinuteSelectedListener;
    }

    /**
     * On minute selected listener
     */
    public interface OnMinuteSelectedListener {
        /**
         * On minute selected *
         *
         * @param hour hour
         */
        void onMinuteSelected(int hour);
    }
}
